package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Pets;
import com.example.entity.User;
import com.example.service.OrderService;
import com.example.service.UserService;
import com.example.utils.ExceptionResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.service.ISampleService;
import com.example.entity.Sample;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-05-10
 */
@RestController
@RequestMapping("/sample")
public class SampleController {

        @Resource
        private ISampleService sampleService;

        @Resource
        private OrderService orderService;

        @Resource
        private UserService userService;

        //新增与删除同一个方法
        @PostMapping
        public ExceptionResult save(@RequestBody Sample sample) {
            return ExceptionResult.success(sampleService.saveOrUpdate(sample));
        }

        //寄养审批方法
        @PostMapping("/pass")
        public ExceptionResult pass(@RequestBody Sample sample) {
                if(sample.getSamplereserva() == 1){
                        User user = userService.getById(sample.getUserid());
                        Map<String,String> map = new HashMap<>();
                        map.put("content","恭喜您已经预约成功本店的宠物寄养服务，感谢您的信任，我们会为您提供优质的服务！");
                        map.put("address",user.getEmail());
                        orderService.order(map);
                }
                if (sample.getSamplereserva() == -1){
                        User user = userService.getById(sample.getUserid());
                        Map<String,String> map = new HashMap<>();
                        map.put("content","非常抱歉，由于本店该服务已满，无法为您提供本次服务，感谢您的信任，我们会尽快扩大规模！");
                        map.put("address",user.getEmail());
                        orderService.order(map);
                }
                return ExceptionResult.success(sampleService.saveOrUpdate(sample));
        }

        //删除
        @DeleteMapping("/{id}")
        public ExceptionResult delete(@PathVariable Integer id) {
            return ExceptionResult.success(sampleService.removeById(id));
        }

        //查询列表
        @GetMapping
        public ExceptionResult findAll() {
            return ExceptionResult.success(sampleService.list());
        }

        //根据id查询单个
        @GetMapping("/{id}")
        public ExceptionResult findOne(@PathVariable Integer id) {
            return ExceptionResult.success(sampleService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public ExceptionResult deleteBatch(@RequestBody List<Integer> ids){
                return ExceptionResult.success(sampleService.removeByIds(ids));
        }

        //获取寄养用户数量
        @GetMapping("/count")
        public ExceptionResult getSampleCount(){
                return ExceptionResult.success(sampleService.getSampleCount());
        }
        //分页查询
        @GetMapping("/page")
        public ExceptionResult findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String username,
                                        @RequestParam(defaultValue = "") String petstype) {
                LambdaQueryWrapper<Sample> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!"".equals(username), Sample::getUsername,username);
                if (!"".equals(petstype)){
                        Integer petstypeid = Integer.valueOf(petstype);
                        queryWrapper.eq(Sample::getPetstypeid,petstype);
                }
            return ExceptionResult.success(sampleService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
}
